﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tools
{
    public static class ToolHelper
    {
        public static void SelectFolder(TextBox textBox, FolderBrowserDialog fbd)
        {
            string path = textBox.Text.Trim();
            if (!string.IsNullOrWhiteSpace(path))
                fbd.SelectedPath = path.Trim();
            DialogResult dialogResult = fbd.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                textBox.Text = fbd.SelectedPath;
            }
        }

        public static void SelectFile(TextBox textBox, OpenFileDialog ofd)
        {
            string path = textBox.Text.Trim();
            if (!string.IsNullOrWhiteSpace(path))
                ofd.InitialDirectory = new FileInfo(path.Trim()).Directory.FullName;
            ofd.Filter = "所有文件(*.*)|*.*";
            DialogResult dialogResult = ofd.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                textBox.Text = ofd.FileName;
            }
        }

        public static bool IsSelectValidate(TextBox txtBox, Label lbl, SelectType type)
        {
            string path = txtBox.Text;
            if (string.IsNullOrWhiteSpace(path))
            {
                lbl.Text = "请选择文件(夹)";
                return false;
            }
            path = path.Trim();
            dynamic info = null;
            switch (type)
            {
                case SelectType.File:
                    info = new FileInfo(path);
                    break;
                case SelectType.Folder:
                    info = new DirectoryInfo(path);
                    break;
                default:
                    break;
            }

            if (!info.Exists)
            {
                lbl.Text = "文件(夹)不存在";
                return false;
            }

            return true;
        }
    }

    public enum SelectType
    {
        File,
        Folder
    }
}
